<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">
                    分配权限
                  </el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParameter.page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pageParameter.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出对话框 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="dialogClose"
      >
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="ruleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <assign-permission v-if="showDialogAssign" ref="permission" :role-id="roleId" @close="showDialogAssign=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, delRole, editRole } from '@/api/setting'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      roleList: [], // 所有角色列表
      pageParameter: {
        page: 1,
        pagesize: 2
      }, // 获取角色列表的需要的参数
      total: 0, // 获取一共多少条
      dialogVisible: false, // 弹出对话框
      ruleForm: {
        name: '',
        description: ''
      }, // 表单数据
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      }, // 表单验证
      isEdit: false, // 点的是否是编辑
      showDialogAssign: false, // 分配权限对话框
      roleId: ''
    }
  },
  created() {
    // 获取所有角色列表
    this.loadRoleList()
  },
  methods: {
    async loadRoleList() {
      const res = await getRoleList(this.pageParameter)
      this.total = res.data.total // 获取一共多少条
      this.roleList = res.data.rows // 获取所有角色列表
    }, // 获取所有角色列表
    async loadAddRole() {
      const res = await addRole(this.ruleForm)
      if (res.code === 10000) {
        this.$message({
          message: res.message,
          type: 'success'
        })
      } else {
        this.$message.error(res.message)
      }
    }, // 添加角色
    async loadDelRole(id) {
      const res = await delRole(id)
      if (res.code === 10000) {
        this.$message({
          message: res.message,
          type: 'success'
        })
      } else {
        this.$message.error(res.message)
      }
    }, // 删除角色
    async loadeditRole() {
      const res = await editRole(this.ruleForm)
      if (res.code === 10000) {
        this.$message({
          message: res.message,
          type: 'success'
        })
      } else {
        this.$message.error(res.message)
      }
    }, // 编辑表单
    handleSizeChange(val) {
      this.pageParameter.pagesize = val
      this.loadRoleList()
    }, // 每页 多少条
    handleCurrentChange(val) {
      this.pageParameter.page = val
      this.loadRoleList()
    }, // 当前页
    addRole() {
      this.dialogVisible = true
    }, // 添加角色
    submit() {
      if (this.isEdit) {
        this.loadeditRole()
      } else {
        this.loadAddRole()
        // 添加用户后跳转到添加的位置
        this.pageParameter.page = Math.ceil(++this.total / this.pageParameter.pagesize)
      }
      this.loadRoleList()
      this.dialogVisible = false
    }, // 添加或编辑的提交
    dialogClose() {
      // 关闭弹窗清空数据
      this.ruleForm = {
        name: '',
        description: ''
      }
    }, // 关闭弹窗清空数据
    delRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loadDelRole(id)
        this.pageParameter.page = Math.ceil(--this.total / this.pageParameter.pagesize)
        this.loadRoleList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }, // 删除角色
    editRole(row) {
      this.isEdit = true
      this.ruleForm = { ...row }
      this.dialogVisible = true
    }, // 编辑角色
    // 用户点击了权限分配
    hAssign(id) {
      // alert(id)
      // 1. 保存角色编号
      //    它会影响子组件中的props,但是，这个传递的过程是异步的
      this.roleId = id
      // 2. 弹层
      this.showDialogAssign = true
      // 3. 手动调用子组件的loadPermissionByRoleId， 去根据最新的roleId获取权限信息
      this.$nextTick(() => {
        this.$refs.permission.loadPermissionByRoleId()
      })
    }
  }
}
</script>
